package quickstart.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import quickstart.model.Person;
import quickstart.model.Resolve;
import quickstart.service.PersonService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public class PersonAction implements Preparable {
    private PersonService service;
    private List<Person> persons;
    private Person person;
    private Resolve resolve;
    

	private Integer id;

    public PersonAction(PersonService service) {
        this.service = service;
    }

    public String execute() {
        this.persons = service.findAll();
        return Action.SUCCESS;
    }

    public String save() {
        this.service.save(person);
        this.person = new Person();
        return execute();
    }

    public String remove() {
        service.remove(id);
        return execute();
    }
    
    public String resolve() {
    	String idStr = ServletActionContext.getRequest().getParameter("id");
    	System.out.println("idStr: "+idStr);
    	this.person = this.service.find(Integer.parseInt(idStr));
        return "resolve";
    }
    
    /**
     * 返回首页选定的问题,并且 分页 展现解决方案列表
     * @return
     */
    public String saveResovle() {
    	Person oldPerson = this.service.find(this.person.getId());
    	this.resolve.setPerson(oldPerson);
    	this.service.save(this.resolve);
    	this.person = this.service.find(this.person.getId());
    	return "resolve";
    }
    
    

    public List<Person> getPersons() {
        return persons;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void prepare() throws Exception {
        if (id != null)
            person = service.find(id);
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    
    public Resolve getResolve() {
		return resolve;
	}

	public void setResolve(Resolve resolve) {
		this.resolve = resolve;
	}
}
